package com.kuang.unsafe;

import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * Author: Zhang Dongwei
 * Date: 2022/3/13 14:22
 * 同理：java.util.ConcurrentModificationException 并发修改异常！
 * 解决方案：
 * 1. Set<String> set = Collections.synchronizedSet(new HashSet<>());
 * 2. Set<String> set = new CopyOnWriteArraySet<>();
 *
 */
public class SetTest {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        //
//        Set<String> set = Collections.synchronizedSet(new HashSet<>());
//        Set<String> set = new CopyOnWriteArraySet<>();
        for (int i = 1; i <= 1000; i++) {
            set.add(UUID.randomUUID().toString().substring(0,5));
            System.out.println(set);
            new Thread(()->{}, String.valueOf(i)).start();
        }
    }
}
